package cn.iocoder.yudao.module.service.controller.admin.assistantgroup;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.service.controller.admin.assistantgroup.vo.*;
import cn.iocoder.yudao.module.service.dal.dataobject.assistantgroup.ServiceAssistantGroupDO;
import cn.iocoder.yudao.module.service.service.assistantgroup.ServiceAssistantGroupService;

@Tag(name = "管理后台 - 客服分组")
@RestController
@RequestMapping("/service/assistant-group")
@Validated
public class ServiceAssistantGroupController {

    @Resource
    private ServiceAssistantGroupService assistantGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建客服分组")
    @PreAuthorize("@ss.hasPermission('service:assistant-group:create')")
    public CommonResult<Long> createAssistantGroup(@Valid @RequestBody ServiceAssistantGroupSaveReqVO createReqVO) {
        return success(assistantGroupService.createAssistantGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客服分组")
    @PreAuthorize("@ss.hasPermission('service:assistant-group:update')")
    public CommonResult<Boolean> updateAssistantGroup(@Valid @RequestBody ServiceAssistantGroupSaveReqVO updateReqVO) {
        assistantGroupService.updateAssistantGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客服分组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('service:assistant-group:delete')")
    public CommonResult<Boolean> deleteAssistantGroup(@RequestParam("id") Long id) {
        assistantGroupService.deleteAssistantGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客服分组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('service:assistant-group:query')")
    public CommonResult<ServiceAssistantGroupRespVO> getAssistantGroup(@RequestParam("id") Long id) {
        ServiceAssistantGroupDO assistantGroup = assistantGroupService.getAssistantGroup(id);
        return success(BeanUtils.toBean(assistantGroup, ServiceAssistantGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客服分组分页")
    @PreAuthorize("@ss.hasPermission('service:assistant-group:query')")
    public CommonResult<PageResult<ServiceAssistantGroupRespVO>> getAssistantGroupPage(@Valid ServiceAssistantGroupPageReqVO pageReqVO) {
        PageResult<ServiceAssistantGroupDO> pageResult = assistantGroupService.getAssistantGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ServiceAssistantGroupRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得客服分组列表")
    @PreAuthorize("@ss.hasPermission('service:assistant-group:query')")
    public CommonResult<List<ServiceAssistantGroupRespVO>> getAssistantGroupList() {
        ServiceAssistantGroupPageReqVO pageReqVO = new ServiceAssistantGroupPageReqVO();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServiceAssistantGroupDO> pageResult = assistantGroupService.getAssistantGroupPage(pageReqVO).getList();
        return success(BeanUtils.toBean(pageResult, ServiceAssistantGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客服分组 Excel")
    @PreAuthorize("@ss.hasPermission('service:assistant-group:export')")
    @OperateLog(type = EXPORT)
    public void exportAssistantGroupExcel(@Valid ServiceAssistantGroupPageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServiceAssistantGroupDO> list = assistantGroupService.getAssistantGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客服分组.xls", "数据", ServiceAssistantGroupRespVO.class,
                BeanUtils.toBean(list, ServiceAssistantGroupRespVO.class));
    }

}